#include <iostream>
#include <thread>
#include <time.h>
#include <unistd.h>
#include "RingQueue.hpp"

void consume(RingQueue<int> &rq)
{
    while (1)
    {
        unique_lock<mutex> ulock(rq.c_mutex);
        while (rq.isEmpty())
        {
            rq.c_con.wait(ulock);
        }
        int ret = rq.pop();
        cout << "get ret:" << ret << endl;
        rq.p_con.notify_one();
        cout << "处理数据中" << endl;
        sleep(1);
    }
}

void product(RingQueue<int> &rq)
{
    while (1)
    {
        unique_lock<mutex> ulock(rq.p_mutex);
        while (rq.isFull())
        {
            rq.p_con.wait(ulock);
        }
        cout << "获取数据中" << endl;
        int data = rand() % 10000;
        sleep(1);
        rq.push(data);
        cout << "creat data:" << data << endl;
        rq.c_con.notify_one();
        sleep(1);
    }
}

int main()
{
    srand(time(nullptr));
    RingQueue<int> rq(5);
    thread c[3];
    for (auto &e : c)
    {
        e = thread(consume, ref(rq));
    }
    thread p[3];
    for (auto &e : p)
    {
        e = thread(product, ref(rq));
    }

    for (auto &e : c)
    {
        e.join();
    }

    for (auto &e : p)
    {
        e.join();
    }
    return 0;
}